import {Body, Controller, HttpStatus, Post, Get, Res, Query} from '@nestjs/common';
import {Response} from 'express';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ApplyUserStatus, ResCode} from '../../common/constants';
import {ApplyFlyerService} from "../../service/apply.flyer.service";
import {ApplyFlyerDto} from "../../dto/apply.flyer.dto";
import {uuid} from "../../common/utils";


@Controller('apply-admin/flyer')
export class ApplyFlyerController{
    constructor(private readonly applyFlyerService: ApplyFlyerService) {
    }

    @Post('/add')
    async addFlyer(@Body() applyFlyerDto: ApplyFlyerDto, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!applyFlyerDto.name || !applyFlyerDto.accountId || !applyFlyerDto.phone || !applyFlyerDto.age || !applyFlyerDto.sex) {
            return new CommonResponseDto(ResCode.ERROR, 'name accountId phone age sex 必填', null)
        }
        try {
            const user  = await this.applyFlyerService.getOneByAccountId(applyFlyerDto.accountId)
            if (user) {
                return new CommonResponseDto(ResCode.ERROR, 'accountId已存在', null)
            }
            applyFlyerDto.status = ApplyUserStatus.NORMAL
            applyFlyerDto.id = uuid(32)
            const result = await this.applyFlyerService.createOne(applyFlyerDto)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit')
    async editFlyer(@Body() applyFlyerDto: ApplyFlyerDto, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!applyFlyerDto.name || !applyFlyerDto.accountId || !applyFlyerDto.phone || !applyFlyerDto.age || !applyFlyerDto.sex) {
            return new CommonResponseDto(ResCode.ERROR, 'name accountId phone age sex 必填', null)
        }
        try {
            if (![ApplyUserStatus.NORMAL, ApplyUserStatus.DELETED].includes(applyFlyerDto.status)) {
                return new CommonResponseDto(ResCode.ERROR, 'status状态不正确', null)
            }
            const user  = await this.applyFlyerService.getOneByAccountId(applyFlyerDto.accountId)
            if (user && user.id !== applyFlyerDto.id) {
                return new CommonResponseDto(ResCode.ERROR, 'accountId已存在', null)
            }
            const result = await this.applyFlyerService.editOne(applyFlyerDto)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Get('/list')
    async getAllFlyerList(@Query() query: {status?: ApplyUserStatus}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const result = await this.applyFlyerService.getAll(query.status)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }
}
